<?php

namespace Futures\Controller;
use Think\Controller;
use Common\Model\OptionModel;
/**
 *  代理商客户控制器--客户信息（上下级关系）、佣金明细、客户订单
 */
class AgentDollarOrderController extends AgentBaseController{

	public function searche_get(){
		//合约搜索
		$option=I('get.option');
		if($option){
			$whereOption['name']=array('like','%'.$option.'%');
			$optionData=M('futures_option')->where($whereOption)->select();
			if($optionData){
				foreach($optionData as $op){
					$opt[]=intval($op['id']);
				}
				$searchstr['option_id']=array('in',$opt);
			}
			$this->assign('option',$option);
		}
		//开始时间搜索
		$start_time=I('get.start_time');
		if($start_time){
			$stime=strtotime($start_time);
			$searchstr['start_time']=array('$gte'=>$stime);
			$this->assign('start_time',$start_time);
		}
		//结束时间搜索
		$end_time=I('get.end_time');
		if($end_time){
			$etime=strtotime($end_time);
			$searchstr['start_time']=array('$lte'=>$etime);
			$this->assign('end_time',$end_time);
		}
		//同时查询
		if($start_time && $end_time){
			$etime = $etime+86400;
			$searchstr['start_time']=array('$gte'=>$stime,'$lte'=>$etime);
		}
		//状态搜索
		$status=I('get.status');
		if($status && $status!=-2){
			$searchstr['status']=intval($status);
			$this->assign('status',$status);
		}
		//方向搜索
		$direction=I('get.direction');
		if($direction && $direction!=-2){
			if($direction == 2){
				$searchstr['direction']=0;
			}else{
				$searchstr['direction']=$direction;
			}
			$this->assign('direction',$direction);
		}
		//查出该ID下的所有下级
		$Model=new OptionModel('Dollarorder');
		ini_set('mongo.long_as_object', 1);
		$uid = session('AGENT.user_id');
		$where['_string'] = "(route like '{$uid}') or (route regexp ',{$uid},') or (route regexp '^{$uid},') or (route regexp ',{$uid}$')";
		$userData=M('follow')->where($where)->select();
		$arr=array();
		foreach($userData as $val){
			$arr[]=$val['id'];
		}
		$searchstr['user_id']=array('in',$arr);
		//当有用户搜索时，覆盖$searchstr['user_id'];
		$name=I('get.name');
		if($name){
			$whereName['nickname']=array('like','%'.$name.'%');
			$whereName['_string'] = " (route like '{$uid}') or (route regexp ',{$uid},') or (route regexp '^{$uid},') or (route regexp ',{$uid}$')";
			$nickData=M('follow')->where($whereName)->select();
			if(!empty($nickData)){
				$nick=array();
				foreach($nickData as $nic){
					$nick[]=$nic['id'];
				}
				$searchstr['user_id']=array('in',$nick);
			}else{
				$searchstr['user_id'] = '';
			}

			$this->assign('name',$name);
		}

		$pagesize=$Model->where($searchstr)->count();
		$page=new \Think\Page($pagesize,10);
		$henData=$Model->where($searchstr)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
		$this->assign('page',$page);


		//组装前台页面需要的数据
		foreach($henData as $key=>$v){
			$usedata=M('follow')->where('id='.$v['user_id'])->find();
			$henData[$key]['nickname']=$usedata['nickname'];
			$optidata=M('futures_option')->where('id='.$v['option_id'])->find();
			$henData[$key]['heyue']=$optidata['name'];
			$henData[$key]['daima']=$optidata['code'];
            $henData[$key]['start_time']=$v['start_time'].'';
            $henData[$key]['end_time']=$v['end_time'].'';
		}
		return $henData;
	}
	public function index(){
		$ouData=$this->searche_get();
		$this->assign('henData',$ouData);
		$this->display();
	}
}
